<?php
    class Duan_model extends CI_Model{
        function __construct(){
            parent::__construct();
            $this->load->database();
        }
        
        function getListDuAnHoanThanh($idUser){
            $query = $this->db->query("SELECT duan.MaDuAn AS idDuAn, TenDuAn, NgayBatDau, NgayDuKienKetThuc, NgayKetThuc, TenLoaiDuAn, TenVaiTro
                                        FROM duan, loaiduan, vaitronhanvienduan, vaitro
                                        WHERE duan.MaTrangThai = '3'
                                        AND duan.MaLoaiDuAn = loaiduan.MaLoaiDuAn
                                        AND duan.MaDuAn = vaitronhanvienduan.MaDuAn
                                        AND vaitronhanvienduan.MaHoSo = '{$idUser}'
                                        AND vaitronhanvienduan.MaVaiTro = vaitro.MaVaiTro
                                    ");
            return $query->result_array();
        }
        
        function getDuAnHoatDong($idUser){
            $query = $this->db->query("SELECT duan.MaDuAn AS idDuAn, TenDuAn, NgayBatDau, NgayDuKienKetThuc, NgayKetThuc, TenLoaiDuAn, TenVaiTro
                                        FROM duan, loaiduan, vaitronhanvienduan, vaitro
                                        WHERE duan.MaTrangThai= '2'
                                        AND duan.MaLoaiDuAn = loaiduan.MaLoaiDuAn
                                        AND duan.MaDuAn = vaitronhanvienduan.MaDuAn
                                        AND vaitronhanvienduan.MaHoSo = '{$idUser}'
                                        AND vaitronhanvienduan.MaVaiTro = vaitro.MaVaiTro
                                    ");
            return $query->result_array();
        }
        
        function saveBaoCaoDinhKy($data){
            
        }
        
        function saveGiaoNhiemVu($data){
            $this->db->insert('nhiemvu', $data);
        }
        
        function saveNhanVienNhiemVu($data){
            $this->db->insert('nhanviennhiemvuduan', $data);
        }
        
        function saveTraoDoi($data){
            $this->db->insert('hoithoaiduan', $data);
        }
        
        function getDanhSachBaoCaoTheoThanhVien($idDuAn, $idThanhVien){
            $query = $this->db->query("SELECT MoTaCongViecHoanThanh, MoTaCongViecKeTiep, NgayBaoCao, MoTaThem, LinkFile, HoTen
                                        FROM baocaodinhky, hosonhanvien
                                        WHERE baocaodinhky.DuAn = {$idDuAn}
                                        AND baocaodinhky.NguoiNop = hosonhanvien.MaHoSo
                                        AND baocaodinhky.NguoiNop = {$idThanhVien}
                                        ");
            return $query->result_array();
        }
        
        function getDanhSachBaoCaoDinhKy($idDuAn){
            $query = $this->db->query("SELECT MoTaCongViecHoanThanh, MoTaCongViecKeTiep, NgayBaoCao, MoTaThem, LinkFile, HoTen
                                        FROM baocaodinhky, hosonhanvien
                                        WHERE baocaodinhky.DuAn = {$idDuAn}
                                        AND baocaodinhky.NguoiNop = hosonhanvien.MaHoSo
                                        ORDER BY MaBaoCao DESC
                                        ");
            return $query->result_array();
        }
        
        function saveBaoCao($data){
            $this->db->insert('baocaodinhky', $data);
        }
        
        function getHoiThoaiTraoDoi($idDuAn){
            $query = $this->db->query("SELECT NoiDung, LinkFile, UserName FROM hoithoaiduan, taikhoancanhan
                                        WHERE NguoiViet = MaHoSo AND TrongDuAn = {$idDuAn}
                                    ");
            return $query->result_array();
        }
        
        
        function getDanhSachNhiemVu($idDuAn){
            $query = $this->db->query("SELECT DISTINCT TenNhiemVu, nhiemvu.MaNhiemVu, NoiDung, ThoiHan, TenLoaiBaoCao, TenDuAn, HoTen
                                        FROM nhiemvu, loaibaocaodinhky , duan, nhanviennhiemvuduan AS nvnvda, hosonhanvien WHERE DuAn = '{$idDuAn}' 
                                        AND LoaiBaoCao = MaLoaiBaoCao AND NguoiNhanNhiemVu = hosonhanvien.MaHoSo
                                        AND nvnvda.MaDuAn = duan.MaDuAn
                                        AND nvnvda.MaNhiemVu = nhiemvu.MaNhiemVu
                                        AND duan.MaDuAn = '{$idDuAn}'
                                        ORDER BY nhiemvu.MaNhiemVu DESC");
            return $query->result_array();
        }
        
        
        function getNhanVienDuAn($idDuAn){
            $query = $this->db->query("SELECT DISTINCT hs.MaHoSo, hs.HoTen FROM vaitronhanvienduan, hosonhanvien AS hs
                                        WHERE hs.MaHoSo = vaitronhanvienduan.MaHoSo
                                        AND MaDuAn = '{$idDuAn}'");
            return $query->result_array();
        }
        
        function getLoaiBaoCao(){
            $query = $this->db->query("SELECT * FROM loaibaocaodinhky");
            return $query->result_array();
        }
        
        function getNhiemVuMoi(){
            $query = $this->db->query("SELECT MaNhiemVu FROM nhiemvu ORDER BY MaNhiemVu DESC LIMIT 1");
            $result = $query->result_array();
            return $idNhiemVu = $result[0]['MaNhiemVu'];
        }
        
        
        function test(){
            $query = $this->db->query("SELECT MaNhiemVu FROM nhiemvu ORDER BY MaNhiemVu DESC LIMIT 1");
            $result = $query->result_array();
            return $idNhiemVu = $result[0]['MaNhiemVu'];
        }
        
        function check($id){
            $query = $this->db->query("SELECT * FROM vaitronhanvienduan WHERE MaHoSo = '{$id}' AND MaVaiTro ='3'");
            $arr = $query->result_array();
            return count($arr);

        }
        
        
        
        
        
        
        
    }
?>